/*
 * @Author: huangtianyang 916072572@qq.com
 * @Date: 2023-08-15 16:29:52
 * @LastEditors: huangtianyang
 * @LastEditTime: 2023-10-09 09:24:14
 * @FilePath: \cxl-admin\src\pages\statisticsManage\register\list\index.ts
 */

import { registerCompanyCityChart, registerCompanyYearChart } from '@/apis/statistics/register'
import BaseChart from '../../baseChart'
import { onMounted, onUnmounted, ref } from 'vue'

export default class StatistiscRegisterData {
    currentYear = ref('')
    registerCity: any
    registerCompany: any
    registerCityOption: any = {
        tooltip: {
            trigger: 'item'
        },
        legend: {
            top: 0,
            left: 'center',
            data: ['CityA', 'CityB', 'CityD', 'CityC', 'CityE']
        },
        series: [
            {
                name: '总量',
                type: 'pie',
                radius: '65%',
                center: ['50%', '50%'],
                selectedMode: 'single',
                data: [
                    { value: 1548, name: 'CityE' },
                    { value: 735, name: 'CityC' },
                    { value: 510, name: 'CityD' },
                    { value: 434, name: 'CityB' },
                    { value: 335, name: 'CityA' }
                ]
            }
        ]
    }
    registerCompanyOption: any = {
        tooltip: {
            trigger: 'axis'
        },
        legend: { bottom: 10, data: [''] },
        grid: {
            left: '3%',
            right: '4%',
            bottom: '50',
            containLabel: true
        },
        toolbox: {},
        xAxis: {
            type: 'category',
            boundaryGap: false,
            data: ['']
        },
        yAxis: {
            type: 'value'
        },
        series: [
            {
                name: '注册',
                type: 'line',
                stack: 'Total',
                data: ['']
            }
        ]
    }
    constructor() {
        const oDate = new Date()
        this.currentYear.value = oDate.getFullYear() + ''

        onMounted(() => {
            this.registerCity = new BaseChart('registerCity')
            this.registerCompany = new BaseChart('registerCompany')
        })
        onUnmounted(() => {
            this.registerCity.destory()
            this.registerCompany.destory()
        })
        this.getCityRegister()
        this.getCompanyRegister(this.currentYear.value)
    }
    //按个城市注册分类
    getCityRegister() {
        registerCompanyCityChart().then(res => {
            if (res.data.value === 200) {
                this.registerCityOption.legend.data.length = 0
                this.registerCityOption.series.length = 0
                // this.registerCityOption.legend.data.push(...productTypeArr)

                this.registerCityOption.series.push({
                    type: 'pie',
                    radius: '65%',
                    center: ['50%', '50%'],
                    selectedMode: 'single',
                    data: []
                })
                console.log(res.data.data)
                res.data.data.forEach((item: any) => {
                    this.registerCityOption.legend.data.push(item.city)
                    this.registerCityOption.series[0].data?.push({
                        value: item.num,
                        name: item.city
                    })
                })
                console.log(this.registerCityOption)
                this.registerCity.init(this.registerCityOption)
            }
        })
    }
    //按企业注册
    getCompanyRegister(year: string) {
        registerCompanyYearChart(year).then(res => {
            if (res.data.value === 200) {
                this.registerCompany.destory()
                this.registerCompany = new BaseChart('registerCompany')
                this.registerCompanyOption.xAxis.data.length = 0
                this.registerCompanyOption.series[0].data.length = 0
                const arr = res.data.data
                arr.sort((x: any, y: any) => x.month - y.month)
                arr.forEach((item: any) => {
                    this.registerCompanyOption.xAxis.data.push(item.month + '月')
                    this.registerCompanyOption.series[0].data?.push(item.num)
                })
                this.registerCompany.init(this.registerCompanyOption)
            }
        })
    }
    //监听选择的年
    onYearChange = (res: any) => {
        this.getCompanyRegister(res)
    }
}
